<?php
require('ParametroGeneral.class.php');
require('FacturaVenta.class.php');
require('Cliente.class.php');


$modo = (isset($_GET['modo'])) ? $_GET['modo']:0;
switch($modo){
    case 1: //Consultar Grid
        // Se lista en Orden Compra con tipo producto = 2
        break;
    case 2: //Insertar
        if(isset($_POST['submit'])){
            $cliId  = $_POST['cliId'];
            $usuId  = $_POST['usuId'];
            $fecAct = date("Ymd");
            $horAct = date("His");
            // idElemento+"#"+prodId+"#"+cant+"#"+prod+"#"+accion+"#"+iocId; |        
            $odc = htmlspecialchars(trim($_POST['elementos']));
            
            $itemOdc = explode("|", $odc);

            for ($i = 0, $count = count($itemOdc); $i < $count; $i++) {
                if(strlen(trim($itemOdc[$i])) >0){
                    $arrayIOC[$i] = (explode("#", $itemOdc[$i]));
                }				
            }
            $array[0] = $cliId;
            $array[1] = 0;
            $array[2] = 0;
            $array[3] = 1;
            $array[4] = 0;
            $array[5] = 0;
            $array[6] = 0;
            $array[7] = $fecAct;
            $array[8] = $horAct;
            $array[9] = $usuId;
            $array[10] = $arrayIOC;
	    $obj    = new FacturaVenta();
            
            if ( $obj->insertar($array) == true)  echo 1;
            else echo 0;
        }
        break;
    case 3: //Modificar
        if(isset($_POST['submit'])){
            $fveId  = $_POST['id'];
            //$usuId  = $_POST['usuId'];
            //$fecAct = date("Ymd");
            //$horAct = date("His");
            // idElemento+"#"+prodId+"#"+cant+"#"+prod+"#"+accion+"#"+iocId; |        
            $odc = htmlspecialchars(trim($_POST['elementos']));
            
            $itemOdc = explode("|", $odc);

            for ($i = 0, $count = count($itemOdc); $i < $count; $i++) {
                if(strlen(trim($itemOdc[$i])) >0){
                    $arrayIOC[$i] = (explode("#", $itemOdc[$i]));
                }				
            }
            $array[0] = $fveId;
            $array[1] = $arrayIOC;
	    $obj    = new FacturaVenta();
            
            if ( $obj->actualizarOrdenFactura($array) == true)  echo 1;
                else echo 0;   
           
        }        
        break;
    case 4://Eliminar
        if(isset($_POST['submit'])){
            $id     = $_POST['id'];
            $obj    = new FacturaVenta();
            if ( $obj->eliminar($id) == true) echo 1;
            else echo 0;
        }             
        break;
    case 5: //Consulta los Datos del cliente y los Ítems de la Orden de Factura
        $cliId  = $_GET['cliId'];
        $fveId  = $_GET['fveId'];
        $obj    = new FacturaVenta();
        $obj->getOrdenFactura($cliId, $fveId);
        break;
    default:
        echo "Debe seleccionar un modo: \n1. Consultar Grid\n2. Insertar\n3. Modificar\n4. Eliminar";
        break;
}
?>
